/**
 * Created by zhangxiaoyu on 2016-04-08.
 */
angular.module('hyt.khdzCtrl',[])

    //通过站点查询账务
  .controller('SearchKhdzCtrl',function($scope,$state,$filter,$ionicLoading,$stateParams){

    //客户名称、电话、开始时间、结束时间
    $scope.khxx={
      khmc:'',
      khdh:'',
      kssj:'',
      jssj:'',
      khmcdh:''
    };

      var t = new Date();
      var iToDay=t.getDate();
      var iToMon=t.getMonth();
      var iToYear=t.getFullYear();
      var iTDate=new Date(iToYear,iToMon,(iToDay-30));
      var khmc=$stateParams.khmc;
      var lxdh=$stateParams.lxdh;
      $scope.khxx.khmc=khmc;
      $scope.khxx.khdh=lxdh;
      if ($scope.khxx.khmc!=''||$scope.khxx.khdh!=''){
          $scope.khxx.khmcdh=khmc+'/'+lxdh;
      }
      $scope.khxx.kssj=$filter('date')(iTDate,'yyyy-MM-dd');
      $scope.showStartDate = function() { //选择日期
          var optionsStart = {
              date: iTDate,
              mode: 'date',
              maxDate: new Date($scope.khxx.jssj) - 10000
          };
          datePicker.show(optionsStart, function(d) {
              if (!isNaN(d.getTime())) {  // valid date
                  $scope.$apply(function () {
                      iTDate=d;
                      $scope.khxx.kssj = $filter('date')(iTDate,'yyyy-MM-dd');
                  });
              }
          });
      };

      var endTime = new Date();
      $scope.khxx.jssj=$filter('date')(endTime,'yyyy-MM-dd');
      $scope.showEndDate = function(){
          var optionsEnd = {
              date : endTime,
              mode : 'date',
              minDate: new Date($scope.khxx.kssj) - 10000
          };
          datePicker.show(optionsEnd,function(d) {
              if (!isNaN(d.getTime())) {  // valid date
                  $scope.$apply(function () {
                      endTime=d;
                      $scope.khxx.jssj = $filter('date')(endTime,'yyyy-MM-dd');
                  });
              }
          });
      };
    //获得该公司的客户列表
    //$scope.getList=function(){
    //  $scope.$on('List.khList',function(){
    //     var list=KhFactory.getKh();
    //     var khList=list.ACTION_INFO.RESULT_LIST;
    //     console.log(khList);
    //  })
    //}

    //点击查询按钮传递参数并跳转到khdzlist页面
    $scope.queryCw=function(khmc,khdh,kssj,jssj){
        if(khmc==''||khdh==''){
            $ionicLoading.show({
                noBackdrop: true,
                template: "请选择客户名称和电话",
                duration: 1500
            });
        }else {
            $state.go('khdzlist',{khmc:khmc,khdh:khdh,kssj:kssj,jssj:jssj});
        }
    }
      //选择联系人
      $scope.chooseLinkman = function (htmlbs) {
          $state.go("linkman",{htmlbs:htmlbs});
      };
  })
  .controller('KhdzListCtrl',function($scope,FhrdzListFactory,ShrdzListFactory,$stateParams,Storage){
    $scope.showloading = true;
    var user_id=Storage.get('user_id');
    var yh_id=Storage.get('yh_id');
    var sid=Storage.get('sid');
    var dw_id=Storage.get('dw_id');
    var zd_id=Storage.get('zd_id');
    //下拉刷新
    $scope.fhrdoRefresh = function(){
        FhrdzListFactory.fhrList($stateParams.khmc,$stateParams.khdh,$stateParams.kssj,$stateParams.jssj,sid,user_id,yh_id,dw_id,zd_id);
        $scope.$broadcast('scroll.refreshComplete');
    };
      $scope.shrdoRefresh = function(){
          ShrdzListFactory.shrList($stateParams.khmc,$stateParams.khdh,$stateParams.kssj,$stateParams.jssj,sid,user_id,yh_id,dw_id,zd_id);
          $scope.$broadcast('scroll.refreshComplete');
      };
    //通过$stateParams接收serachkhdz页面传过来的参数，初始化客户财务列表
    FhrdzListFactory.fhrList($stateParams.khmc,$stateParams.khdh,$stateParams.kssj,$stateParams.jssj,sid,user_id,yh_id,dw_id,zd_id);
      ShrdzListFactory.shrList($stateParams.khmc,$stateParams.khdh,$stateParams.kssj,$stateParams.jssj,sid,user_id,yh_id,dw_id,zd_id);
    $scope.$on('FhrdzList.fhrdzUpdated', function() {

      var fhrdzListFactory=FhrdzListFactory.getFhr();
      $scope.fhrdzData = fhrdzListFactory;
        console.log($scope.fhrdzData);
       if($scope.fhrdzData!=undefined){
           $scope.khmc=$scope.fhrdzData.khmc;
           var wjfy_y=fhrdzListFactory.yf_xf_ys+fhrdzListFactory.yf_yj_ys+fhrdzListFactory.yf_hf_ys+fhrdzListFactory.yf_ycdk_ys+fhrdzListFactory.thf_ys+fhrdzListFactory.shf_ys+fhrdzListFactory.bf_ys+fhrdzListFactory.dsksxf_ys+fhrdzListFactory.xxf_yf+fhrdzListFactory.yf_df_yf+fhrdzListFactory.dshk_yf;
           var wjfy_s=fhrdzListFactory.yf_xf_sh+fhrdzListFactory.yf_yj_sh+fhrdzListFactory.yf_hf_sh+fhrdzListFactory.yf_ycdk_sh+fhrdzListFactory.thf_sh+fhrdzListFactory.shf_sh+fhrdzListFactory.bf_sh+fhrdzListFactory.dsksxf_sh+fhrdzListFactory.xxf_sf+fhrdzListFactory.yf_df_sf+fhrdzListFactory.dshk_sf;
           $scope.fhrwjfy=wjfy_y-wjfy_s;
       }
      $scope.showloading=false;

    });
      $scope.$on('ShrdzList.shrdzUpdated', function() {

          var shrdzListFactory=ShrdzListFactory.getShr();
          $scope.shrdzData = shrdzListFactory;
          console.log($scope.shrdzData);
          if($scope.shrdzData!=undefined){
              $scope.khmc=$scope.shrdzData.khmc;
              var wjfy_y=shrdzListFactory.yf_tf_ys+shrdzListFactory.dshk_ys;
              var wjfy_s=shrdzListFactory.yf_tf_sh+shrdzListFactory.dshk_sh;
              $scope.shrwjfy=wjfy_y-wjfy_s;
          }
          $scope.showloading=false;

      });

  });

